package com.jming.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jming.yygh.cmn.client.DictFeignClient;
import com.jming.yygh.hosp.repository.HospitalRepository;
import com.jming.yygh.hosp.service.HospitalService;
import com.jming.yygh.model.hosp.Hospital;
import com.jming.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jming
 * @date 2021-04-22
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {

        // 1、把map集合转换成对象
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        // 2、获取查询结果
        String hosCode = hospital.getHosCode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHosCode(hosCode);

        // 3、判断是否存在数据，如果存在则进行修改，否则添加
        if (hospitalExist != null) {
            hospitalExist.setIsDeleted(0);
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setStatus(hospitalExist.getStatus());
            hospitalExist.setCreateTime(hospitalExist.getCreateTime());
            hospitalRepository.save(hospitalExist);
        } else {
            hospital.setStatus(1);
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hosCode) {
        return hospitalRepository.getHospitalByHosCode(hosCode);
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        // 创建pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建Example对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        // 创建条件匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);

        // 调用查询方法
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        // 获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item -> this.setHospitalHospType(item));

        // 返回结果
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {

        // 根据id查询要修改的信息
        Hospital hospital = hospitalRepository.findById(id).get();

        // 设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());

        // 保存修改
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {

        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHospType(hospitalRepository.findById(id).get());

        // 处理数据(医院基本信息，医院等级)
        result.put("hospital", hospital);
        result.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);

        // 返回结果
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        return hospitalRepository.getHospitalByHosCode(hoscode).getHosName();
    }

    @Override
    public List<Hospital> findByHosName(String hosName) {

        return hospitalRepository.findHospitalByHosNameLike(hosName);

    }

    @Override
    public Map<String, Object> item(String hosCode) {

        Map<String, Object> result = new HashMap<>();

        // 医院详情
        Hospital hospital = this.setHospitalHospType(this.getByHoscode(hosCode));
        result.put("hospital", hospital);

        // 预约规则
        result.put("bookingRule", hospital.getBookingRule());

        // 不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }

    /**
     * 获取查询list集合，遍历进行医院等级封装
     *
     * @param hospital hospital对象
     * @return 封装后的hospital对象
     */
    private Hospital setHospitalHospType(Hospital hospital) {

        // 根据dictCode和value获取医院等级名称
        String hosTypeSrting = dictFeignClient.getName("hosType", hospital.getHosType());

        // 查询省、市、地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        hospital.getParam().put("hosTypeSrting", hosTypeSrting);
        return hospital;
    }
}
